#include"cpp-httplib/httplib.h"
#include"search.hpp"
#include"log.hpp"

const std::string& home_path="./www.root";

int main()
{
    Search s;
    s.InitiaSearch();
    httplib::Server svr;
    //std::cout<<"hello"<<std::endl;
    svr.set_base_dir(home_path.c_str());
    //std::cout<<"world"<<std::endl;
    
    svr.Get("/s", [&s](const httplib::Request &req, httplib::Response &res) {
        if(!req.has_param("wd"))
        {
            res.set_content("无关键字,无法搜索","text/plain; charset=utf-8");
            return;
        }
        std::string wd=req.get_param_value("wd");
        //std::cout<<"用户在搜索 word: "<<wd<<std::endl;
        LOG(NORMAL,"搜索中 word: "+wd);
        std::string json_string; 
        s.search(wd,&json_string);
        res.set_content(json_string,"application/json");
    });
    //std::cout<<"hello"<<std::endl;
    svr.listen("0.0.0.0",8086);
    //std::cout<<"world"<<std::endl;
    return 0;
}